import { _decorator, AudioClip, Component, tween, Tween, UIOpacity, UITransform, v3, Vec3 } from 'cc';
const { ccclass, property, executionOrder } = _decorator;

import CardFunctions from "../Common/CardFunctions";
import { GameData } from "../Data/GameData";
import Board from "../Game/Board";
import SS_TimerManager from "./SS_TimerManager";
import GameStorage from "../Data/GameStorage";
import { Consts } from "../Data/Consts";
import { bdlMgr } from "./SS_BundleManager";
import GamePanel from "../UI/GamePanel";
import { SS_AudioManager } from './SS_AudioManager';
import { SS_Settings } from '../Data/SS_Settings';
import CommonFunction from '../utils/CommonFunction';

// // 慢一点执行
@ccclass('SS_MapManager')
// @executionOrder(102)
export default class SS_MapManager extends Component {
    static Instance: SS_MapManager;

    onLoad() {
        SS_MapManager.Instance = this;
    }

    async initMap() {
        GameData.arrCards = [];
        GameData.arrGrids = [];
        for (let x = 0; x < GameData.col; x++) {
            GameData.arrGrids[x] = [];
            for (let y = 0; y < GameData.row; y++) {
                const id = 0;
                CardFunctions.CreateGrid(id, x, y).then((scrGrid) => {
                    GameData.arrGrids[x][y] = scrGrid;
                });
            }
        }

        this.InitCard();

        // if (!GameStorage.data.isShowWujin && CardFunctions.获取当前关卡是否是无尽第1关(GameData.Get_CurrentLevel())) {
        //     GameStorage.data.isShowWujin = true;
        //     GameStorage.saveData();
        //     await CardFunctions.PlayShowWujin();
        // }

        let t = CardFunctions.GetAllBlockGroup();
        CardFunctions.ShuffleArray(GameData.currentLevelData_d);

        // 初始三个如果id一样就交换
        // CardFunctions.初始三个id如果一样就交换();

        await this.InitCreateBlockGroup();

        // this.InitADBlock();
        // this.InitStopBlock();

        await this.InitBoard();
        await this.开场动画_up();

        // if (GameStorage.data.guide_index === 1) {
        //     CardFunctions.PlayGuide_1();
        // }

        // CardFunctions.禁止操作(false);
    }

    async InitCard() {
        GameData.arrCards = [];
        for (let x = 0; x < GameData.col; x++) {
            // GameData.arrCards[x] = [];
            for (let y = 0; y < GameData.row; y++) {
                const id = CardFunctions.GetCardId();
                CardFunctions.CreateCard(x, y, id).then((card) => {
                    GameData.arrCards.push(card);
                });
            }
        }
    }

    async InitBoard() {
        let levelData = GameData.Get_LevelData_up();

        for (let index = 0; index < levelData.length; index++) {
            const layer_data = levelData[index];
            // console.log("layer_data", layer_data);

            for (let index2 = 0; index2 < layer_data.length; index2++) {
                const board_data = layer_data[index2];
                // console.log("board_data------------", board_data);

                const x = board_data.x;
                const y = board_data.y;
                const layer = index;
                const _layout = board_data.l;
                const _cardList = board_data.c;


                const board: Board = await CardFunctions.CreateBoard(x, y, layer, _layout, _cardList);

                for (let index = 0; index < _cardList.length; index++) {
                    const id = _cardList[index];
                    if (CardFunctions.IsCardId(id)) {
                        GameData.total_card_count++;
                    }
                }
            }
        }

        CardFunctions.SetAllBoardCover();
    }

    async InitCreateBlockGroup() {
        for (let index = 0; index < 3; index++) {
            await CardFunctions.随机创建一个BlockGroup(index);
        }

        CardFunctions.创建下一个BlockGroup();
    }

    async InitStopBlock() {
        const data = GameData.Get_LevelData_middle();

        if (data) {
            for (let index = 0; index < data.length; index++) {
                let item_data = data[index];
                await CardFunctions.CreateStopBlock(item_data.x, item_data.y, 1);
            }
        }

        GamePanel.Instance.Refresh_Button_摧毁障碍();
    }
    async InitADBlock() {
        let item = CardFunctions.获取解锁格子记录();

        for (let index = 0; index < item.unlock.length; index++) {
            const isunlock = item.unlock[index];
            if (isunlock === false) {
                let map_data = GameData.UnlockData[index];
                for (let index2 = 0; index2 < map_data.length; index2++) {
                    let item_map_data = map_data[index2];
                    let block = await CardFunctions.CreateStopBlock(item_map_data.x, item_map_data.y, 2);
                    block.node.getComponent(UIOpacity).opacity = 0;
                }
            }
        }
    }

    async 开场动画_up() {
        let d1 = 800;
        let d2 = 4;
        for (let index = 0; index < GameData.arrBoard.length; index++) {
            const board: Board = GameData.arrBoard[index];
            board.node.setPosition(board.node.position.x, board.pos.y + d1, board.node.position.z);
            Tween.stopAllByTarget(board.node);
        }

        for (let index = 0; index < GameData.arrBoard.length; index++) {
            const board: Board = GameData.arrBoard[index];

            tween(board.node)
                .to(0.3, { position: new Vec3(board.node.position.x, board.pos.y - d2, board.node.position.z) })
                .to(0.1, { position: v3(board.node.position.x, board.pos.y, board.node.position.z) })
                .call(() => {
                })
                .start();

            // bdlMgr.getRes<AudioClip>(Consts.BundleName.AUDIO, `sound/关卡开始阵列石块下落音效`, AudioClip).then(async (audio) => {
            //     SS_AudioManager.Instance.playOneShot(audio, SS_Settings.curMusicV);
            // });

            await SS_TimerManager.AwaitTime(0.02);
        }

        await SS_TimerManager.AwaitTime(0.4);
    }
}

/** 调试 */
window["SS_MapManager"] = SS_MapManager;